<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Groups/classes/PhpFox_ComponentMyGroups.class.php, $Author:  $ */



/** Component for view my groups
 * @version $Id: PhpFox_ComponentCategories.class.php
 *  Max Kovrigovich
 * @package module.groups
 */
class PhpFox_Mod_Groups_ComponentMyGroups extends BaseComponent
{
    /** Processing method
     */

    function process()
    {
    	App::membersOnly();

        //check permissions
        $oSecurityService = App::getModuleService('Account', 'Security');
		/* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
		$oSecurityService->checkPerm('groups', array('read_only', 'full'));

        $oGroupsService = $this->_oModule->getService('Groups');
        /* @var $oGroupsService PhpFox_Mod_Groups_ServiceGroups */

        $oReq = &App::getRequest();
        /* @var $oReq Request */

        $oUrl = &App::getUrl();
        /* @var $oUrl Url */

        $oTpl = $this->_oModule->getTemplate();

        $sUser = $oReq->get('name','');
        if($sUser != '')
            $oUser = $oSecurityService->getUserByName($sUser);
        else
            $oUser = $this->getParam('user');

        if(!$oUser)
            App::gotoUrl('public.login');

        $iGroupId = $oReq->getInt('id', 0);
        $iDeleteId = $oReq->getInt('delete', 0);
        if($iGroupId)
        {
            if($oGroupsService->isMember($iGroupId, $oUser->aData['user'], $iDeleteId)
                &&
               !$oGroupsService->isOwner($iGroupId, $oUser->aData['user'])
            )
            {
                $oGroupsService->deleteFromGroup($iDeleteId);
                App::gotoUrl('public.groups.mygroups');
            }
        }

        App::loadClass('Pager');
        $iPageSize = App::getParam('public.pagesize');

        list($aItems, $iCnt) = $oGroupsService->getMembership($oUser->aData['user'], $oReq->getInt('page', 1), $iPageSize);

        $iPage = $oReq->getInt('page', 1);
        $oPager = &new Pager($iCnt, $iPage, $iPageSize);

        $iFrom = $oPager->getOffset($iPage, $iPageSize);
        $iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize);

        $oTpl->assign(array(
            'sUser'     => $oUser->aData['user'],
            'iCnt'      => $iCnt,
            'iFrom'     => ($iCnt) ? ($iFrom + 1) : 0,
	        'iTo'       => $iTo,
            'aItems'    => $aItems,
            'aPaging'       => $oPager->getInfo($oUrl),
            'sImageUrl' 	=> App::getParam('groups.pic.url'),
        ));
        $sHtml = $oTpl->fetch('MyGroups.html');
        return $sHtml;
    }
}
?>